/**
 * Created by zhangxiaoyu on 16/4/11.
 */
angular.module('hyt.clcxCtrl',[])
    .controller('SearchCphCtrl',function($scope,$state,CphFactory,$ionicPopup,Storage,$ionicLoading) {
        //选择车牌号
        $scope.che={
            cph:'',
            xl:''
        };
        //$scope.chooseCph = function () {
        //    if($scope.cphList==''||$scope.cphList==null){
        //        $ionicLoading.show({
        //            noBackdrop: true,
        //            template: "本站点暂无可选车牌号",
        //            duration: 1500
        //        });
        //    }else {
        //        var alertPopup = $ionicPopup.alert({
        //            title: '请选择车牌号',
        //            scope: $scope,
        //            template: '<ion-list  ng-repeat="item in cphList"><ion-radio ng-bind="item.cph" style="text-align: center;margin-top: 3px;" ng-click="choose(item.cph,item.cl_id)"></ion-radio></ion-list>',
        //            okText: '取消'
        //        });
        //        alertPopup.then(function (res) {
        //            if (res) {
        //
        //            } else {
        //
        //            }
        //        });
        //        $scope.choose = function (cph, cl_id) {
        //            $scope.che.cl_id = cl_id;
        //            $scope.che.cph = cph;
        //            alertPopup.close();
        //        };
        //    }
        //
        //};
        var dw_id=Storage.get("dw_id");
        var sid=Storage.get('sid');
        var user_id=Storage.get('user_id');
        var yh_id=Storage.get('yh_id');
        var zd_id=Storage.get('zd_id');

        //CphFactory.queryCph(dw_id,sid,user_id,yh_id,zd_id);
        //获得车牌号列表
        //$scope.$on('List.cphList',function(){
        //    var list=CphFactory.getCph();
        //    $scope.cphList=list.ACTION_INFO.RESULT_LIST;
        //    console.log($scope.cphList);
        //})
        $scope.Search=function(cph,xl){
            var zt=$("#zt option:selected").val();
            $state.go('chelianglist',{cph:cph,xl:xl,clyxzt:zt});
        }
        $scope.SearchCph=function(cph,xl){
            if(cph==''){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请输入车牌号",
                    duration: 1500
                });
            }else {
                $state.go('carlist',{cph:cph,xl:xl,clyxzt:zt});
            }
        }
        $scope.searchScanCph=function(cph){
            if(cph==''){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请输入车牌号",
                    duration: 1500
                });
            }else {
                $state.go('scanCarList',{cph:cph});
            }
        }
    })
    .controller('CphListCtrl',function($scope,CphFactory,$stateParams,Storage){
        var dw_id=Storage.get("dw_id");
        var sid=Storage.get('sid');
        var user_id=Storage.get('user_id');
        var yh_id=Storage.get('yh_id');
        var zd_id=Storage.get('zd_id');
        $scope.showloading = true;
        $scope.bmmc=$stateParams.bmmc;
        var xl=$stateParams.xl;
        var clyxzt=$stateParams.clyxzt;
        CphFactory.queryCph(dw_id,sid,user_id,yh_id,zd_id,$stateParams.cph,xl,clyxzt);
        $scope.$on('List.cphList',function(){
            var list=CphFactory.getCph();
            $scope.cphList=list.data;
            $scope.showloading=false;
            console.log($scope.cphList);
        });
        $scope.doRefresh = function(){
            CphFactory.queryCph(dw_id,sid,user_id,yh_id,zd_id,$stateParams.cph,xl,clyxzt);
            $scope.$broadcast('scroll.refreshComplete');
        };

        //上拉加载更多
        $scope.loadMore = function(){
            CphFactory.getMoreCph(dw_id,sid,user_id,yh_id,zd_id,$stateParams.cph,xl,clyxzt);
            $scope.$broadcast('scroll.infiniteScrollComplete');
        };
        $scope.hasNextPage = function() {
            return CphFactory.hasNextPage();
        };
    })
    //车辆查询装单列表
    .controller('ZcdListCtrl',function($scope,ZcdListFactory,$stateParams,Storage,$state){
        var dw_id=Storage.get("dw_id");
        var sid=Storage.get('sid');
        var user_id=Storage.get('user_id');
        var yh_id=Storage.get('yh_id');
        var zd_id=Storage.get('zd_id');
        $scope.showloading = true;
        ZcdListFactory.getZcdList(sid,user_id,yh_id,$stateParams.cl_id,$stateParams.cph,dw_id,zd_id);
        $scope.$on('zcdList.zcdUpdated', function() {
            var zcdListFactory=ZcdListFactory.getZcd();
            $scope.zcdData = zcdListFactory.data;
            $scope.getClzt=function(clzt){
                switch (clzt){
                    case 1 :
                        $scope.clzt = '装车';
                        break;
                    case 2 :
                        $scope.clzt = '发车';
                        break;
                    case 3 :
                        $scope.clzt = '到货';
                        break;
                    default :
                        break;
                }
                return $scope.clzt;
            };
            console.log($scope.zcdData);
            $scope.showloading=false;
        });
        //下拉刷新
        $scope.doRefresh = function(){
            ZcdListFactory.getZcdList(sid,user_id,yh_id,$stateParams.cl_id,$stateParams.cph,dw_id,zd_id);
            $scope.$broadcast('scroll.refreshComplete');
        };
        $scope.ClGPSLoc=function(){
            $state.go("GPSLocation",{cl_id:$stateParams.cl_id});
        }

    })
    //车辆查询承运单列表
    .controller('ClcxListCtrl',function($scope,ClcxListFactory,$stateParams,Storage,$ionicHistory){
        var dw_id=Storage.get("dw_id");
        var sid=Storage.get('sid');
        var user_id=Storage.get('user_id');
        var yh_id=Storage.get('yh_id');
        $scope.cph=$stateParams.cph;
        $scope.showloading = true;
        ClcxListFactory.getClcxList($stateParams.id,$stateParams.cph,dw_id,sid,user_id,yh_id);
        console.log(Storage.get("dw_id"));
        $scope.$on('ClcxList.clcxUpdated', function() {
            var clcxListFactory=ClcxListFactory.getClcx();
            $scope.clcxData = clcxListFactory.data;
            console.log($scope.clcxData);
            $scope.showloading=false;
        });
        //下拉刷新
        $scope.doRefresh = function(){
            ClcxListFactory.getClcxList($stateParams.id,$stateParams.cph,dw_id,sid,user_id,yh_id);
            $scope.$broadcast('scroll.refreshComplete');
        };

        //上拉加载更多
        $scope.loadMore = function(){
            ClcxListFactory.getMoreClcx($stateParams.id,$stateParams.cph,dw_id,sid,user_id,yh_id);
            $scope.$broadcast('scroll.infiniteScrollComplete');
        };

        //下一页
        $scope.hasNextPage =function(){
            return ClcxListFactory.hasNextPage();
        }
        $scope.myGoBack = function() {
            $ionicHistory.goBack();
        };

    })
.controller('ClGPSCtrl',function($scope,$stateParams,ClGPSFactory,Storage,$ionicHistory){
    //百度地图定位
    var sid=Storage.get('sid');
    var user_id=Storage.get('user_id');
    var yh_id=Storage.get('yh_id');
    var dw_id=Storage.get('dw_id');
    var cl_id=$stateParams.cl_id;
    ClGPSFactory.getGPS(cl_id,sid,user_id,yh_id,dw_id);
    $scope.$on('ClGPS.GPSUpdated',function(){
        var GPSLocation = ClGPSFactory.getResult();
        console.log(GPSLocation);
        if(GPSLocation.clzb_x!=''&&GPSLocation!=''&&GPSLocation.clzb_y!=''){
            var lat = GPSLocation.clzb_y;
            var long = GPSLocation.clzb_x;
            function initialize() {
                var map = new BMap.Map("map");
                var point = new BMap.Point(long, lat);
                map.addOverlay(marker);
                map.centerAndZoom(point, 15);
                var myIcon = new BMap.Icon("img/huoche.png", new BMap.Size(34,32));
                var marker = new BMap.Marker(point,{icon:myIcon});  // 创建标注
                map.addOverlay(marker);// 将标注添加到地图中
                var geoc = new BMap.Geocoder();

                map.addEventListener("click", function (e) {
                    var pt = e.point;
                    geoc.getLocation(pt, function (rs) {
                        var addComp = rs.addressComponents;
                        var opts = {
                            width: 200,     // 信息窗口宽度
                            height: 60,     // 信息窗口高度
                        }
                        var infoWindow = new BMap.InfoWindow("地址：" + addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber, opts);  // 创建信息窗口对象
                        marker.addEventListener("click", function () {
                            map.openInfoWindow(infoWindow, point); //开启信息窗口
                        });
                    });
                });
            }

            $(function () {
                initialize();
            })
        }else {
            $("#map").text("无定位信息");
            $("#map").css({"text-align":"center","font-size":"17px","margin-top":"44px"});
        }

    });
    $scope.myGoBack = function() {
        $ionicHistory.goBack();
    };
})